package main.java.com.dao.impl;

import main.java.com.config.BaseDao;
import main.java.com.dao.LoginDao;
import main.java.com.entity.Employee;

import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginDaoImpl extends BaseDao implements LoginDao {
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;

    /**
     * 登录
     * @param name 账号
     * @param password 密码
     * @return
     */
    @Override
    public List<Employee> login(String name, String password) {
        List<Employee> employees = new ArrayList<>();
        conn = this.getConnection();
        String sql = "select * from employee where username = ? and password = ?";
        try {
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, name);
            psmt.setString(2, password);
            rs = psmt.executeQuery();
            Employee employee = new Employee();
            while (rs.next()) {
                employee.setId(rs.getLong("id"));
                employee.setName(rs.getString("name"));
                employee.setPassword(rs.getString("password"));
                employee.setPhone(rs.getString("phone"));
            }
            if(employee.getId() != null){
                employees.add(employee);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return employees;
    }
}
